#!/bin/bash
# installation elk

# 官网 elastic.co
# 下载 https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.3-linux-x86_64.tar.gz
#        https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.3-linux-x86_64.tar.gz.sha512
#          shasum -a 512 -c elasticsearch-7.9.3-linux-x86_64.tar.gz.sha512
# 下载 https://artifacts.elastic.co/downloads/logstash/logstash-7.9.3.tar.gz 7.9.3
# 下载 https://artifacts.elastic.co/downloads/kibana/kibana-7.9.3-linux-x86_64.tar.gz

. ./public_functions &>/dev/null #公共函数库
. /tmp/public_functions &>/dev/null #公共函数库

##1 二进制安装elk
elk_Install_centos7() {

  readonly e_Version="elasticsearch-7.9.3-linux-x86_64.tar.gz"  # 压缩包名称
  readonly e_Catalog="elasticsearch-7.9.3"		     # 解压后的目录
  readonly k_Version="kibana-7.9.3-linux-x86_64.tar.gz"         # 压缩包名称
  readonly k_Catalog="kibana-7.9.3-linux-x86_64"             # 解压后的目录
  readonly l_Version="logstash-7.9.3.tar.gz"                    # 压缩包名称
  readonly l_Catalog="logstash-7.9.3"                        # 解压后的目录

#  if [[ -d /data/elk/"$e_Catalog" ]] && [[ -d /data/elk/"$k_Catalog" ]] && [[ -d /data/elk/"$l_Catalog" ]]; then
#     logTips "ELK maybe already exist" "(ELK可能已安装,请手动检查)" #1-20#
#     exit
#  fi

  logW "1. 添加用户 es" #1-20#
  logY "useradd es" #1-20#
        useradd es
    
    if [[ $? -eq 0 || $? -eq 9 ]]; then
       logG "success ↑↑↑" #1-20#
       echo
    else
       logR "fail ↑↑↑       -->> return $?" #1-20#
       exit
    fi

  logW "2. 创建目录 /data/elk" #1-20#
  logY "mkdir -p /data/elk" #1-20#
        mkdir -p /data/elk
        checkTF #21#

  logW "3." #1-20#
  for i in "$e_Version" "$k_Version" "$l_Version"
  do
           
      logW "开始查找并解压 $i " #1-20#
      logY "find / -name $i" #1-20#
           find / -path /proc -prune -o -name $i | grep -v '/proc' #显示文件路径
           find / -path /proc -prune -o -name $i | grep -Eo --silent "$i" #判断查找是否成功

	   if [[ $? == 0 ]]; then
	      software_url=$(find / -path /proc -prune -o -name $i | grep -v '/proc') #文件路径赋值给变量
              logW "\n--- 解压 $software_url"
              logY "tar -xzf $software_url -C /data/elk/"
                   tar -xzf $software_url -C /data/elk/
                   checkTF #21#
           fi
  done
  
  logW "4. 修改目录 /data 归属"
  logY "chown -R es:es /data" #1-20#
        chown -R es:es /data
        checkTF #21#

  logW "5. 修改配置文件 /etc/security/limits.conf" #1-20#

    es_soft="es soft nofile 65535"
    es_hard="es hard nofile 65537"

    for i in "$es_soft" "$es_hard"
    do
      logY "grep --quiet \"^$i$\" /etc/security/limits.conf || sed -i \"\$a $i\" /etc/security/limits.conf" #1-20#
           grep --quiet "^$i$" /etc/security/limits.conf || sed -i "\$a $i" /etc/security/limits.conf
           checkTF #21#
    done

   logW "6. 修改配置文件/data/elk/$e_Catalog/config/elasticsearch.yml"
     
        e_config_url="/data/elk/$e_Catalog/config/elasticsearch.yml" #elasticsearch配置文件路径
        e_var1="action.auto_create_index: .monitoring*,.watches,.triggered_watches,.watcher-history*,.ml*" #官方要求

        for i in "$e_var1"
	do
            logY "grep --quiet \"^$i$\" $e_config_url || sed -i \"\$a $i\" $e_config_url" #1-20#
	         grep --quiet "^$i$" $e_config_url || sed -i "\$a $i" $e_config_url
                 if [[ $? == 0 ]]; then
	            logG "替换成功"
	         else
	            logR "替换失败"
	         fi
        done

	grep --quiet "^vm\.max_map_count=300000$" /etc/sysctl.conf || sed -i "\$a vm\.max_map_count=300000" /etc/sysctl.conf
	sysctl -p >/dev/null && echo #调整虚拟内存数值

  logW "7. 启动Elasticsearch" #1-20#
  logY "su - es -c \"/data/elk/$e_Catalog/bin/elasticsearch -d\"" #1-20#
       su - es -c "/data/elk/$e_Catalog/bin/elasticsearch -d"
       checkTF #21#

  logW "8. 修改配置文件/data/elk/$k_Catalog/config/kibana.yml"

        k_config_url="/data/elk/$k_Catalog/config/kibana.yml" #kibana配置文件路径
        k_var1="server.port: 5601"
        k_var2="server.host: \"0.0.0.0\""
        k_var3="elasticsearch.hosts: \[\"http://localhost:9200\"\]"
        k_var4="kibana.index: \".kibana\""
        k_var5="kibana.defaultAppId: \"home\""

        for i in "$k_var1" "$k_var2" "$k_var3" "$k_var4" "$k_var5"
	do
	    k_y=$(echo $i | cut -d" " -f1)
            logY "sed -i \"/$k_y/a $i\" $k_config_url" #1-20#
	         sed -i "/$k_y/a $i" $k_config_url
                 if [[ $? == 0 ]]; then
	            logG "替换成功"
	         else
	            logR "替换失败"
	         fi
        done
  
  logW "9. 启动Kibana" #1-20#
  logY "su - es -c \"/data/elk/$k_Catalog/bin/kibana &\"" #1-20#
       source /etc/profile
       su - es -c "/data/elk/$k_Catalog/bin/kibana &"
       checkTF #21#

  logW "10. 放行5601端口"
  get_Firewalld_status #23#
    if [[ $? -eq 0 ]]; then
      logY "firewall-cmd --zone=public --add-port="5601"/"tcp" --permanent" #1-20#
      firewall-cmd --zone=public --add-port="5601"/"tcp" --permanent 2>/dev/null
      firewall-cmd --reload &>/dev/null
      echo ""
    else
      logR "防火墙未开启" #1-20#
      echo ""
    fi

  l_url="/data/elk/logstash-7.9.3"
  logW "11. 生成logstash 默认配置文件"
  logY "cp $l_url/config/logstash-sample.conf $l_url/config/logstash.conf"
       cp $l_url/config/logstash-sample.conf $l_url/config/logstash.conf
       checkTF #21#
  
  logW "PS: 可使用 curl -X GET "localhost:9200/?pretty" 访问验证 elasticsearch"
  logW "PS: 请在浏览器访问 http://{{ip}}:5601 验证 kibana"
  logW "PS: 可使用$l_url/bin/logstash -f $l_url/config/logstash.conf & 启动logstash" && echo
}

check_Version #25#

systemtype=$(uname -a | grep -Eo '(ubuntu|centos7)')

if [[ $systemtype == centos7 ]]; then 
   elk_Install_centos7  ##1

elif [[ $systemtype == ubuntu ]]; then
     elk_Install_ubuntu  ##2

fi
